/*
 * @Date         : 2022-06-08 09:49:50
 * @LastEditors  : zu1662
 * @LastEditTime : 2024-05-24 15:01:47
 * @Description  : 文件 Controller
 *
 * Copyright © 2022 by zu1662, All Rights Reserved.
 */
import {
  Controller,
  HttpCode,
  HttpStatus,
  Post,
  UploadedFile,
  Query,
  Param,
  Delete,
  UploadedFiles,
  Body,
  Get,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ENUM_FILE_TYPE } from 'src/utils/file/file.constant';
import { UploadFileMultiple, UploadFileSingle } from 'src/utils/file/file.decorator';
import { Response } from 'src/utils/response/response.decorator';
import { PaginationService } from 'src/utils/pagination/service/pagination.service';
import { FileService } from './file.service';
import { FileEntity } from './file.entity';
import { FileListDto } from './dto/file.list.dto';
import { AuthJwtGuard } from 'src/auth/auth.decorator';
import { IResponse } from 'src/utils/response/response.interface';

@ApiTags('File')
@Controller({
  version: '1',
  path: 'file',
})
export class FileController {
  constructor(
    private readonly fileService: FileService,
    private readonly paginationService: PaginationService
  ) {}

  // 获取所有文件
  @Response('file.list')
  @AuthJwtGuard()
  @HttpCode(HttpStatus.OK)
  @Get('/list')
  async list(@Query() queryParams: FileListDto): Promise<IResponse> {
    const { page, perPage, search, sort, availableSearch, availableSort, ...otherParams } =
      queryParams;
    const skip: number = await this.paginationService.skip(page, perPage);

    // 获取分页，排序数据
    const result = await this.fileService.findAll({
      skip,
      page,
      perPage,
      sort,
      search,
      availableSearch,
      filters: otherParams,
    });
    // 总条数
    const totalData = result.totalData;
    // 根据总条数分页
    const totalPage: number = await this.paginationService.totalPage(totalData, perPage);
    return {
      totalData,
      totalPage,
      currentPage: page,
      perPage,
      availableSearch,
      availableSort,
      data: result.data,
    };
  }

  // 获取单个文件详情
  @Response('file.getFile')
  @AuthJwtGuard()
  @HttpCode(HttpStatus.OK)
  @Get('/:id')
  async getFile(@Param('id') id: string): Promise<FileEntity> {
    return await this.fileService.findById(id);
  }

  // 上传单个文件
  @Response('file.upload')
  // @AuthJwtGuard()
  @UploadFileSingle('file')
  @HttpCode(HttpStatus.OK)
  @Post('/upload')
  async upload(@UploadedFile() file, @Query('unique') unique): Promise<FileEntity> {
    return await this.fileService.uploadFile(file, unique);
  }

  // 上传多个文件
  @Response('file.upload')
  // @AuthJwtGuard()
  @UploadFileMultiple('file', ENUM_FILE_TYPE.IMAGE)
  @HttpCode(HttpStatus.OK)
  @Post('/upload-muti')
  async uploadMuti(@UploadedFiles() files, @Query('unique') unique): Promise<FileEntity[]> {
    const fileEntities = [];
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      const fileEntity = await this.upload(file, unique);
      fileEntities.push(fileEntity);
    }
    return fileEntities;
  }

  // 删除单个文件
  @Response('file.delete')
  // @AuthJwtGuard()
  @HttpCode(HttpStatus.OK)
  @Delete('/delete/:id')
  async delete(@Param('id') id): Promise<void> {
    await this.fileService.deleteById(id);
  }

  // 删除多个文件
  @Response('file.delete')
  @AuthJwtGuard()
  @HttpCode(HttpStatus.OK)
  @Delete('/delete-muti')
  async deleteMuti(@Body() ids: string[]): Promise<void> {
    for (let i = 0; i < ids.length; i++) {
      const id = ids[i];
      await this.delete(id);
    }
  }
}
